using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace FUSE
{
    public class Radar
    {
        private Texture2D PlayerDotImage;
        private Texture2D PlanetDotImage;
        private Texture2D SunDotImage;
        private Texture2D WormholeDotImage;
        private Texture2D BlackholeDotImage;
        private Texture2D StartPlanetDotImage;
        private Texture2D EndPlanetDotImage;

        private Texture2D AsteroidDotImage;
        private Texture2D AsteroidBeltDotImage;

        private Rectangle radarRectangle;
        private Rectangle wormholeRectangle;
        private Rectangle playerRectangle;


        public Vector2 radarSize = new Vector2(300, 300);
        public Vector2 radarObjectSize = new Vector2(20, 20);

        private Texture2D RadarImage;
        private Texture2D VisualForceImage = null;
        private Texture2D objectCirle;
        // Local coords of the radar image's center, used to offset image when being drawn
        private Vector2 RadarImageCenter;
       
        private Vector2 WormholeImageCenter;
        private Vector2 PlayerImageCenter;
        private Vector2 SunImageCenter;
        private Vector2 PlanetImageCenter;
        private Vector2 BlackholeImageCenter;
        private Vector2 startPlanetImageCenter;
        private Vector2 endPlanetImageCenter;

        // Distance that the radar can "see"
        private const float RadarRange = 700.0f;

        // Radius of radar circle on the screen
        private const float RadarScreenRadius = 100.0f;

        // This is the center position of the radar hud on the screen. 
        static Vector2 RadarCenterPos = new Vector2(700, 500);
        
        public Radar(ContentManager Content, Texture2D gotObjectCircle)
        {
            
            PlayerDotImage = Content.Load<Texture2D>("NormalShipRoughSmall");
            PlanetDotImage = Content.Load<Texture2D>("newPlanetSmall");
            SunDotImage = Content.Load<Texture2D>("xPlose");
            WormholeDotImage = Content.Load<Texture2D>("wormhole");
            BlackholeDotImage = Content.Load<Texture2D>("xPlose");
            RadarImage = Content.Load<Texture2D>("mapBackground01");
            StartPlanetDotImage = Content.Load<Texture2D>("Planet01Small");
            EndPlanetDotImage = Content.Load<Texture2D>("Planet01Small");
         
            //AsteroidDotImage = Content.Load<Texture2D>(enemyDotPath);
            //AsteroidBeltDotImage = Content.Load<Texture2D>(radarImagePath);


            radarRectangle = new Rectangle(0, 0, (int)radarSize.X, (int)radarSize.Y);


            RadarImageCenter = new Vector2(RadarImage.Width / 2.0f, RadarImage.Height / 2.0f);
            PlayerImageCenter = new Vector2(PlayerDotImage.Width / 2.0f, PlayerDotImage.Height / 2.0f);
            PlanetImageCenter = new Vector2(PlanetDotImage.Width / 2.0f, PlanetDotImage.Height / 2.0f);
            SunImageCenter = new Vector2(SunDotImage.Width / 2.0f, SunDotImage.Height / 2.0f);
            BlackholeImageCenter = new Vector2(BlackholeDotImage.Width / 2.0f, BlackholeDotImage.Height / 2.0f);
            WormholeImageCenter = new Vector2(WormholeDotImage.Width / 2.0f, WormholeDotImage.Height / 2.0f);
            startPlanetImageCenter = new Vector2(StartPlanetDotImage.Width / 2.0f, StartPlanetDotImage.Height / 2.0f);
            endPlanetImageCenter = new Vector2(EndPlanetDotImage.Width / 2.0f, EndPlanetDotImage.Height / 2.0f);
        }

        public void Draw(SpriteBatch spriteBatch,  Map mapToScan, Ship ship )
        {
            
            GalaxyComponent galaxyObject =null;
           // RadarImage = mapToScan.visualization;
            // The last parameter of the color determines how transparent the radar circle will be
            spriteBatch.Draw(RadarImage, RadarCenterPos, null, new Color(100, 100, 100, 150), 0.0f, RadarImageCenter, 2 * (RadarScreenRadius / RadarImage.Height), SpriteEffects.None, 0.0f);
            
            //If visual forces havent been drawn
            if (VisualForceImage == null)
            {
                VisualForceImage = mapToScan.visualForcesTex;

            }
            //spriteBatch.Draw(VisualForceImage, RadarCenterPos, null, new Color(100, 100, 100, 150), 0.0f, RadarImageCenter, 2 * (RadarScreenRadius / RadarImage.Height), SpriteEffects.None, 0.0f);
  
            //For every object with mass in the galaxy table check proximity);
            foreach (DictionaryEntry entry in mapToScan.galaxyComponentsTable)
            {
  
                Debug.Assert(entry.Key != null);

                galaxyObject = (GalaxyComponent) entry.Value;

                Vector2 diffVect = (galaxyObject.ComponentBounds.centerPOS) - ship.Position;
                float distance = diffVect.Length();

                // Check if enemy is within RadarRange
                if ( distance < RadarRange )
                {
                    // Scale the distance from world coords to radar coords
                    diffVect *= RadarScreenRadius / RadarRange;

                    // Offset coords from radar's center
                    diffVect += RadarCenterPos;


                    if (galaxyObject is Wormhole)
                    {
                        // Draw enemy dot on radar

                        spriteBatch.Draw(WormholeDotImage, diffVect, null, new Color(0, 255, 0), 0.0f, WormholeImageCenter, .3f,
                                         SpriteEffects.None, 0.0f);
                    }
                    else if (galaxyObject is Planet)
                    {

                        // Draw enemy dot on radar
                        spriteBatch.Draw(PlanetDotImage, diffVect, null, new Color(0, 155, 155), 0.0f, PlanetImageCenter, .3f,
                                        SpriteEffects.None, 0.0f);

                    }
                    else if (galaxyObject is Sun)
                    {
                        // Draw enemy dot on radar
                        spriteBatch.Draw(SunDotImage, diffVect, null, new Color(155, 155, 0), 0.0f, SunImageCenter, .3f,
                                        SpriteEffects.None, 0.0f);
                    }
                    else if (galaxyObject is Blackhole)
                    {
                        // Draw enemy dot on radar
                        spriteBatch.Draw(BlackholeDotImage, diffVect, null, new Color(255, 0, 0), 0.0f, BlackholeImageCenter, .3f,
                                        SpriteEffects.None, 0.0f);
                    }
                    else if (galaxyObject is StartPlanet)
                    {
                        // Draw enemy dot on radar
                        spriteBatch.Draw(StartPlanetDotImage, diffVect, null, new Color(100, 20, 200), 0.0f, startPlanetImageCenter, .3f,
                                        SpriteEffects.None, 0.0f);
                    }
                    else if (galaxyObject is EndPlanet)
                    {
                        // Draw enemy dot on radar
                        spriteBatch.Draw(EndPlanetDotImage, diffVect, null, new Color(20, 160, 50), 0.0f, endPlanetImageCenter, .3f,
                                        SpriteEffects.None, 0.0f);
                    }
                        /*
                    else if (galaxyObject is Asteroid)
                    {
                        // Draw enemy dot on radar
                        spriteBatch.Draw(AsteroidDotImage, diffVect, Color.White);
                    }
                    else if (galaxyObject is AsteroidBelt)
                    {
                        // Draw enemy dot on radar
                        spriteBatch.Draw(AsteroidBeltDotImage, diffVect, Color.White);
                    }
                         * */
                }
            }
 

            // Draw player
            spriteBatch.Draw(PlayerDotImage, RadarCenterPos, null, new Color(255, 255, 255), 0.0f, PlayerImageCenter, .3f,
                            SpriteEffects.None, 0.0f);
        }
    }
}
